package com.okilo.online.app.apt

import androidx.recyclerview.widget.RecyclerView
import com.okilo.online.app.R
import com.okilo.online.app.tools.apt.OkiloRecyclerViewAdapter
import com.okilo.online.app.tools.apt.OkiloViewHolderHelper
import com.okilo.online.app.weight.font.OkiloTextView
import org.json.JSONObject

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2025/3/3 17:49
 * @Annotation：申贷期限适配器
 */
class OkiloApplyTermApt(recyclerView: RecyclerView?) : OkiloRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.okilo_act_apply_term_item) {
    override fun setItemChildListener(helper: OkiloViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.okiloApplyTermText)
    }
    override fun fillData(helper: OkiloViewHolderHelper, position: Int, model: JSONObject) {
        val okiloApplyTermText = helper.getView<OkiloTextView>(R.id.okiloApplyTermText)
        okiloApplyTermText.text = String.format(getString(R.string.okilo_order_status_loan_days),model.optString("day"))

        when(model.optBoolean("isSelect", false)){
            true -> {
                okiloApplyTermText.setBackgroundResource(R.drawable.okilo_apply_term_item_select_bg)
                okiloApplyTermText.setTextColor(getColor(R.color.okilo_color_white))
                okiloApplyTermText.setFontType(OkiloTextView.FontType.RBT_SEMIBOLD)
            }
            else -> {
                okiloApplyTermText.setBackgroundResource(R.drawable.okilo_apply_term_item_normal_bg)
                okiloApplyTermText.setTextColor(getColor(R.color.okilo_color_ff747378))
                okiloApplyTermText.setFontType(OkiloTextView.FontType.RBT_MEDIUM)
            }
        }
    }

    /**
     * 获取选中的Item
     */
    fun findSelectedItem(): JSONObject? {
        return (0 until itemCount).asSequence()
            .mapNotNull { index -> getItem(index) }
            .firstOrNull { it.optBoolean("isSelect", false) }
    }
}